package co.yixiang.hw.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.hw.domain.SupplierUserApply;
import co.yixiang.hw.domain.YxUser;
import co.yixiang.hw.service.SupplierUserApplyService;
import co.yixiang.hw.service.SupplierUserService;
import co.yixiang.hw.service.YxUserService;
import co.yixiang.hw.service.dto.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.function.Supplier;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2020-02-09
*/
@Api(tags = "hyUser管理")
@RestController
@RequestMapping("/api/yxUser")
public class YxUserController extends BaseController {

    private final YxUserService yxUserService;
    private final PasswordEncoder passwordEncoder;
    private final SupplierUserApplyService supplierUserApplyService;
    private final SupplierUserService supplierUserService;


    public YxUserController(YxUserService yxUserService,PasswordEncoder passwordEncoder,SupplierUserApplyService supplierUserApplyService,SupplierUserService supplierUserService) {
        this.yxUserService = yxUserService;
        this.passwordEncoder = passwordEncoder;
        this.supplierUserApplyService = supplierUserApplyService;
        this.supplierUserService = supplierUserService;
    }

    @Log("检查用户是否存在，不存在自动注册")
    @ApiOperation(value = "检查用户存在并自动注册",response = YxUserDto.class)
    @PostMapping(value = "/checkUserAndRegister")
    @AnonymousAccess
    public ResponseEntity<Object> checkUserAndRegister(@RequestBody JSONObject params){
        String phone = params.getString("phone");
        JSONObject userInfo = params.getJSONObject("userInfo");
        return new ResponseEntity<>(yxUserService.checkUserAndRegister(params),HttpStatus.OK);
    }

    @Log("申请注册为供应商")
    @ApiOperation(value = "申请注册为供应商",response = SupplierUserApplyDto.class)
    @PostMapping(value = "/applySupplier")
    public ResponseEntity<Object> applySupplier(@RequestBody JSONObject params){
        Long userId = getUserId();
        String name = params.getString("name");
        String companyName = params.getString("companyName");
        Assert.notNull(name,"真实姓名不能为空");
        Assert.notNull(companyName,"公司名不能为空");
        SupplierUserApply apply = new SupplierUserApply();
        apply.setAuditStatus(1);
        apply.setCompanyName(companyName);
        apply.setUserId(userId);
        apply.setUserName(name);
        apply.setIsDel(0);
        apply.setCreateTime(System.currentTimeMillis());
        SupplierUserApplyDto supplierUserApplyDto = supplierUserApplyService.create(apply);
        return new ResponseEntity<>(supplierUserApplyDto,HttpStatus.OK);
    }

    @Log("获取代理商级别")
    @ApiOperation(value = "获取代理商级别",response = Integer.class)
    @PostMapping(value = "/supplier/level")
    @AnonymousAccess
    public ResponseEntity<Object> getSupplierLevel(@RequestBody JSONObject params){
        Integer level = 0;
        Long userId = getUserId();
        if(userId != -1L) {
            SupplierUserQueryCriteria criteria = new SupplierUserQueryCriteria();
            criteria.setUserId(userId);
            List<SupplierUserDto> supplierUserDtos = supplierUserService.queryAll(criteria);
            if(supplierUserDtos != null && supplierUserDtos.size() > 0){
                SupplierUserDto supplierUserDto = supplierUserDtos.get(0);
                level = supplierUserDto.getLevel();
            }
        }
        return new ResponseEntity<>(level,HttpStatus.OK);
    }

    @Log("获取申请审核状态")
    @ApiOperation(value = "获取申请审核状态",response = SupplierUserApplyDto.class)
    @PostMapping(value = "/applyInfo")
    public ResponseEntity<Object> getApplyInfo(@RequestBody JSONObject params){
        Long userId = getUserId();
        SupplierUserApply applyInfo = null;
        if(userId != 1L) {
            applyInfo = supplierUserApplyService.getNewApplyByUserId(userId);
        }
        return new ResponseEntity<>(applyInfo,HttpStatus.OK);
    }
}
